<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { login } from '@/api/user';
import { useUserStore } from '@/store';

const router = useRouter();
const userStore = useUserStore();

// 表单数据
const loginForm = reactive({
  username: '',
  password: '',
});

// 表单校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为6位', trigger: 'blur' },
  ],
};

const loading = ref(false);
const loginFormRef = ref(null);

// 提交表单
const handleSubmit = async () => {
  if (!loginFormRef.value) return;
  
  try {
    await loginFormRef.value.validate();
    
    loading.value = true;
    
    try {
      // 调用真实登录API
      const res = await login({
        username: loginForm.username,
        password: loginForm.password
      });
      
      // 保存登录状态
      userStore.setToken(res.data.token);
      userStore.setUserInfo(res.data.userInfo);
      
      ElMessage.success('登录成功');
      router.push('/dashboard');
    } catch (error) {
      console.error('登录失败:', error);
      ElMessage.error(error.message || '登录失败，请检查账号密码');
    } finally {
      loading.value = false;
    }
  } catch (error) {
    console.error('表单验证失败');
  }
};
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">棋牌室管理系统</div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="rules"
        label-position="top"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            prefix-icon="Lock" 
            show-password
            @keyup.enter="handleSubmit"
          />
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            @click="handleSubmit" 
            class="login-button"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <div class="login-tips">
        默认账号: admin<br>
        默认密码: admin123
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7f9;
}
.login-box {
  width: 400px;
  padding: 40px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.login-title {
  font-size: 24px;
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
  font-weight: bold;
}
.login-form {
  margin-top: 20px;
}
.login-button {
  width: 100%;
}
.login-tips {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: #999;
  line-height: 1.5;
}
</style> 
